<style>
.center {
  width: 100vw;   /* 使用视口单位设置宽度为屏幕宽度 */
  height: 100vh;  /* 使用视口单位设置高度为屏幕高度 */
  background-image: url('../assets/1.png');
  background-size: cover;  /* 背景图片等比例缩放以填充容器 */
  background-position: center;  /* 将背景图片位置设置为居中 */
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

h1 {
  font-size: 30px;
  color: black;
}

.logon {
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  /* position: relative;
  overflow: hidden; */
  width: 768px;
  max-width: 100%;
  min-height: 520px;
  margin-top: 20px;
  display: flex;
  background: -webkit-linear-gradient(to right, #8dbef4, #79f4da); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to right, #b8f1c0, #67fa5c); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}

.overlaylong {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaylongleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  transform: translateX(100%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaylongright {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaytitle {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
}


.overlaytitleH2 {
  font-size: 30px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitleP {
  font-size: 15px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitleleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
}

.overlaytitleright {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(-100%);
  transition: transform 0.6s ease-in-out;
}

.overlaytitle-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.overlaytitle-Signup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.buttongohs {
  width: 180px;
  height: 40px;
  border-radius: 50px;
  border: 1px solid #fff;
  color: #fff;
  font-size: 15px;
  text-align: center;
  line-height: 40px;
  margin-top: 40px;
}

.overlaylongH2 {
  font-size: 25px;
  color: black;
  /* width: 250px; */
}

.overlaylong-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.overlaylong-Signup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

input {
  background-color: #eee;
  border: none;
  padding: 12px 15px;
  margin: 10px 0;
  width: 240px;
}
h3{
  font-size: 10px;
  margin-top: 10px;
  cursor: pointer;
}
.inupbutton{
  background-color: #29eac4;
  border: none;
  width: 180px;
  height: 40px;
  border-radius: 50px;
  font-size: 15px;
  color: #fff;
  text-align: center;
  line-height: 40px;
  margin-top: 10px;
}
</style>

<template>
  <div class="center">
    <div class="logon">
      <div :class="overlaylong">
        <div class="overlaylong-Signin" v-if="disfiex == 0">
          <h2 class="overlaylongH2">登录</h2>
          <input type="text" placeholder="用户名" v-model="username">
          <input type="text" placeholder="密码" v-model="password">
          <button class="inupbutton" @click="login()">确认</button>
        </div>
        <div class="overlaylong-Signup" v-if="disfiex == 1">
          <h2 class="overlaylongH2">注册</h2>
          <el-input
              placeholder="用户名"
              suffix-icon="el-icon-folder-opened"
              v-model="newusername" style="width: 30vh;margin-bottom: 8px">
          </el-input>
          <el-input
              placeholder="密码"
              suffix-icon="el-icon-folder-opened"
              v-model="newpassword" style="width: 30vh;margin-bottom: 8px">
          </el-input>
          <el-input
              placeholder="邮箱"
              suffix-icon="el-icon-folder-opened"
              v-model="email" style="width: 30vh;margin-bottom: 8px">
          </el-input>
          <el-input
              placeholder="手机号码"
              suffix-icon="el-icon-phone"
              v-model="phone" style="width: 30vh;margin-bottom: 8px">
          </el-input>
          <el-date-picker
              v-model="Birthday"
              type="date"
              placeholder="选择日期"
              style="width: 30vh;margin-bottom: 8px">
          </el-date-picker>
          <div style="display: flex; align-items: center">
            <h2 style="font-size: 2vh;">性别：</h2>
            <el-radio-group v-model="newGender">
              <el-radio-button label="男" ></el-radio-button>
              <el-radio-button label="女" ></el-radio-button>
              <el-radio-button label="其他" ></el-radio-button>
            </el-radio-group>
          </div>
          <button class="inupbutton" @click="register()">注册</button>
        </div>

      </div>
      <div :class="overlaytitle">
        <div class="overlaytitle-Signin" v-if="disfiex == 0">
          <h2 class="overlaytitleH2">没有账号？</h2>
          <p class="overlaytitleP">
            输入你的个人信息，进入图书馆
          </p>
          <div class="buttongohs" @click="Signin">注册</div>
        </div>
        <div class="overlaytitle-Signup" v-if="disfiex == 1">
          <h2 class="overlaytitleH2">进入图书馆</h2>
          <p class="overlaytitleP">输入用户名以及密码以进入图书馆</p>
          <div class="buttongohs" @click="Signup">登录</div>
        </div>
      </div>
    </div>

    <el-dialog title="你好管理员" :visible.sync="dialogFormVisible" center>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="toStore()">借阅图书</el-button>
        <el-button type="primary" @click="toController()">后台管理</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Cookies from 'js-cookie';
import axios from "axios";

export default {
  data() {
    return {
      userid: 0,
      username: "",
      password: "",
      newusername: "",
      newpassword: "",
      email: "",
      phone: "",
      newGender: "",
      is_admin: true,
      dialogFormVisible: false,
      overlaylong: 'overlaylong',
      overlaytitle: 'overlaytitle',
      Birthday: "",
      disfiex: 0
    }
  },
  methods: {
    toController() {
      this.$router.push({name: 'home'});
      Cookies.set('username', this.username, {expires: 7, path: ''});
      Cookies.set('password', this.password, {expires: 7, path: ''});
      Cookies.set('userid', this.userid, {expires: 7, path: ''});
      Cookies.set('is_admin', this.is_admin, {expires: 7, path: ''});
    },
    toStore() {
      // this.$router.push({ name: 'store' , query: {
      //     username: this.username,
      //     password: this.password
      //   }});
      // 设置Cookies

      // 导航到存储页
      this.$router.push({name: 'userborrowview'});
      Cookies.set('username', this.username, {expires: 7, path: ''});
      Cookies.set('password', this.password, {expires: 7, path: ''});
      Cookies.set('userid', this.userid, {expires: 7, path: ''});
      Cookies.set('is_admin', this.is_admin, {expires: 7, path: ''});
    },
    register() {
      if (this.newpassword == "" || this.newusername == "") {
        alert("请输入用户名以及密码！");
      } else {
        axios.post('http://127.0.0.1:5000/v1/users/isExist', {username: this.newusername}, {headers: {'Content-Type': 'application/json'}})
            .then(response => {
              // 检查状态码
              if (response.data.msg == "User exist") {
                alert("User exist");
                this.newpassword = '';
                this.newusername = '';
              } else {
                axios.post('http://127.0.0.1:5000/v1/users/register', {
                  username: this.newusername,
                  password: this.newpassword
                }, {headers: {'Content-Type': 'application/json'}})
                    .then(response => {
                      // 检查状态码
                      if (response.data.status === 200) {

                        alert("注册成功！");
                        this.username = this.newusername;
                        this.password = '';
                        this.newpassword = '';
                        this.newusername = '';
                        this.Birthday = '';
                        this.newGender = '';
                        this.email = '';
                        this.phone = '';
                        // 跳转到产品页面

                      } else {
                        alert("未知的响应状态，请检查后端服务。");
                      }
                    })
                    .catch(error => {
                      if (error.response && error.response.data) {
                        // 显示后端返回的错误消息
                        alert(error.response.data.msg);
                      } else {
                        console.error('Error:', error);
                      }
                    });
              }
            })
            .catch(error => {
              if (error.response && error.response.data) {
                // 显示后端返回的错误消息
                alert(error.response.data.msg);
              } else {
                console.error('Error:', error);
              }
            });
      }
    },
    login() {
      if (this.password == "" || this.username == "") {
        alert("请输入用户名以及密码！");
      } else {
        axios.post('http://127.0.0.1:5000/v1/users/login', {
          username: this.username,
          password: this.password
        }, {headers: {'Content-Type': 'application/json'}})
            .then(response => {
              // 检查状态码
              if (response.data.status === 200) {
                // alert(response.data.msg); // 显示登录成功消息
                // 假设 response.data.data 是您获得的 JWT
                const token = response.data.data.token;
                this.userid = response.data.data.userid;
                this.is_admin = response.data.data.is_admin;
                // 设置 cookie，并制定有效期为 7 天
                Cookies.set('token', token, {expires: 7, path: ''});
                if (response.data.msg == 'Login successful and you are the admin') {
                  this.dialogFormVisible = true;
                } else {
                  Cookies.set('username', this.username, {expires: 7, path: ''});
                  Cookies.set('password', this.password, {expires: 7, path: ''});
                  Cookies.set('userid', this.userid, {expires: 7, path: ''});
                  Cookies.set('is_admin', this.is_admin, {expires: 7, path: ''});
                  this.$router.push({
                    name: 'userborrowview',
                    query: {
                      username: this.username,
                      password: this.password
                    }
                  },);
                }

                // 跳转到产品页面

              } else {
                alert("未知的响应状态，请检查后端服务。");
              }
            })
            .catch(error => {
              if (error.response && error.response.data) {
                // 显示后端返回的错误消息
                alert(error.response.data.msg);
              } else {
                console.error('Error:', error);
              }
            });
      }
    },
    Signin() {
      this.overlaylong = "overlaylongleft"
      this.overlaytitle = "overlaytitleright"
      setTimeout(() => {
        this.disfiex = 1
      }, 200)
    },
    Signup() {
      this.overlaylong = "overlaylongright"
      this.overlaytitle = "overlaytitleleft"

      setTimeout(() => {
        this.disfiex = 0
      }, 200)

    }
  }
}
</script>